#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int countCompleteSubarrays(vector<int>& nums) {
        int res = 0;
        int n = nums.size();
        unordered_set<int> countDiff;
        for (auto& e : nums) countDiff.insert(e);
        int diff = countDiff.size();
        unordered_map<int, int> hash;
        for (int left = 0, right = 0; right < n; ++right)
        {
            int cur = nums[right];
            ++hash[cur];
            if (hash[cur] == 1)
            {
                --diff;
                while (!diff)
                {
                    res += n - right;
                    if (--hash[nums[left++]] == 0)
                        ++diff;
                }
            }
        }
        return res;
    }
};